<?php

/**
 * 前台 新闻 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: news.class.php 47 2011-07-18 05:29:51Z zwt007 $
 * http://www.t-mac.org；
 */
class news extends Model
{

    private $article_table;
    private $category_table;
    private $addonarticle_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->article_table = '#@__article';
        $this->category_table = '#@__category';
        $this->addonarticle_table = '#@__addonarticle';
    }

    /**
     * 获取所有资讯
     * @param string $url
     * @param string $type
     * @return <type>
     */
    public function getArticleList($url = null, $cat_id)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->article_table WHERE type='post' AND cat_id = '$cat_id'");
        if ($url == '')
            $url = PHP_SELF . '?m=news&cid=' . $cat_id . '&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(12);
        $limit = $pages->getSqlLimit();
        $sql = "SELECT article_id , title, time, attachments, click_count FROM $this->article_table WHERE type='post' AND cat_id = '$cat_id' ORDER BY orderby ASC, article_id DESC LIMIT $limit";
        $rs = $this->db->getAll($sql);        
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['time'] = date('Y-m-d', $v['time']);
            }
        }

        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无资讯! ";
        }

        $this->assign('rs', $rs);
        $this->assign('pageCurrent', $pages->getNowPage());
        $this->assign('page', $pages->show());
        $this->assign('ErrorMsg', $ErrorMsg);
        return $rs;
    }

    /**
     * 取出资讯无分页
     * @param <type> $cat_id
     * @return <type>
     */
    public function getCategoryList($cat_id)
    {
        $sql = "SELECT * FROM $this->category_table WHERE cat_pid = '$cat_id' ORDER BY cat_order DESC, cat_id ASC";
        $rs = $this->db->getAll($sql);        
        return $rs;
    }

}